﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using hDualPaneLib.Controller;
using hDualPaneLib.Model;
using System.Windows.Input;
using System;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;
using System.Collections.Specialized;

namespace hDualPane
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainFrame : Window
    {
        private FileManager _fileManager;
        private bool _currentPane;

        private void test()
        {
            foreach (object ob in lstLeft.Items)
            {
                System.Diagnostics.Debug.WriteLine(ob.GetType());
                //(ob as FileSummary).
                ListViewItem test = new ListViewItem();
            }
        }


        public MainFrame()
        {
            //Start up with both panels show C: Drive
            _fileManager = new FileManager("C:\\");
            InitializeComponent();
            stxtLeft.Text = _fileManager.LeftPath;
            stxtRight.Text = _fileManager.RightPath;
            _currentPane = true;
            //focusPane(DataTypeHelper.ManagerPanel.LeftPane);
            //ListViewItem test = lstLeft.ItemContainerGenerator.ContainerFromIndex(0) as ListViewItem;
            //lstLeft.ItemContainerGenerator.StatusChanged += leftChange;
        }

        private void focusPane(DataTypeHelper.ManagerPanel pane)
        {
            if (pane == DataTypeHelper.ManagerPanel.LeftPane)
            {
                (lstRight.ItemContainerGenerator.ContainerFromIndex(_fileManager.RightIndex) as ListViewItem).IsSelected = false;
                (lstLeft.ItemContainerGenerator.ContainerFromIndex(_fileManager.LeftIndex) as ListViewItem).Focus();
                (lstLeft.ItemContainerGenerator.ContainerFromIndex(_fileManager.LeftIndex) as ListViewItem).IsSelected = true;
            }
            else
            {
                (lstLeft.ItemContainerGenerator.ContainerFromIndex(_fileManager.LeftIndex) as ListViewItem).IsSelected = false;
                (lstRight.ItemContainerGenerator.ContainerFromIndex(_fileManager.RightIndex) as ListViewItem).Focus();
                (lstRight.ItemContainerGenerator.ContainerFromIndex(_fileManager.RightIndex) as ListViewItem).IsSelected = true;
            }
        }

        private void mnuTop_About_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("HxH Dual Panel File Manager",
                "About hDualPane",
                MessageBoxButton.OK,
                MessageBoxImage.Information);
        }

        private void toggleStatusBar(object sender, RoutedEventArgs e)
        {
            if ((sender as MenuItem).IsChecked)
            {
                rowMainStatus.Height = new GridLength(20);
            }
            else
            {
                rowMainStatus.Height = new GridLength(0);
            }
        }

        private void toggleCommandBar(object sender, RoutedEventArgs e)
        {
            if ((sender as MenuItem).IsChecked)
            {
                rowMainCommand.Height = new GridLength(20);
            }
            else
            {
                rowMainCommand.Height = new GridLength(0);
            }
        }

        private void exitApp(object sender, RoutedEventArgs e)
        {
            MessageBoxResult msgResult = MessageBox.Show("You are about to close hDualPane?",
                "Please confirm your action",
                MessageBoxButton.YesNo,
                MessageBoxImage.Question);

            switch (msgResult)
            {
                case MessageBoxResult.Yes:
                    this.Close();
                    break;
            }
        }

        private void globalKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                if (_currentPane)
                {
                    _fileManager.focusPane(DataTypeHelper.ManagerPanel.RightPane, lstLeft.SelectedIndex);
                    focusPane(DataTypeHelper.ManagerPanel.RightPane);
                }
                else
                {
                    _fileManager.focusPane(DataTypeHelper.ManagerPanel.LeftPane, lstRight.SelectedIndex);
                    focusPane(DataTypeHelper.ManagerPanel.LeftPane);
                }
                _currentPane = !_currentPane;
            }
        }

        private void itemDoubleClick(object sender, RoutedEventArgs e)
        {
            navigateToItem(sender as ListViewItem);
        }

        private void itemKeyEnter(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                navigateToItem(sender as ListViewItem);
            }
        }

        private void navigateToItem(ListViewItem item)
        {
            ItemInfo info = (item.Content as ItemInfo);
            _fileManager.LeftIndex = lstLeft.SelectedIndex;
            if (info.Pane == DataTypeHelper.ManagerPanel.LeftPane)
            {
                stxtLeft.Text = _fileManager.LeftPath;
                lstLeft.ItemContainerGenerator.StatusChanged += leftChange;
            }
            else
            {
                stxtRight.Text = _fileManager.RightPath;
            }
            _fileManager.processItem(info, info.Pane);
            lstLeft.scr
        }

        private void leftChange(object sender, EventArgs e)
        {
            
            if (lstLeft.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
            {
                lstLeft.ScrollIntoView(lstLeft.Items[_fileManager.LeftIndex]);
                (lstLeft.ItemContainerGenerator.ContainerFromIndex(_fileManager.LeftIndex) as ListViewItem).Focus();
                (lstLeft.ItemContainerGenerator.ContainerFromIndex(_fileManager.LeftIndex) as ListViewItem).IsSelected = true;
                lstLeft.ItemContainerGenerator.StatusChanged -= leftChange;
            }
        }

        public ObservableCollection<ItemInfo> LeftList
        {
            get
            {
                return _fileManager.LeftList;
            }
        }

        public ObservableCollection<ItemInfo> RightList
        {
            get
            {
                return _fileManager.RightList;
            }
        }
    }
}
